<template>
  <div class="demands-container">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="部门名称">
          <el-input
            v-model="searchForm.departName"
            placeholder="请输入部门名称"
            clearable
            @clear="handleSearch"
          ></el-input>
        </el-form-item>
        <el-form-item label="岗位名称">
          <el-input
            v-model="searchForm.stationName"
            placeholder="请输入岗位名称"
            clearable
            @clear="handleSearch"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"

    >
      <el-table-column
        prop="recruitDetail.station"
        label="需求岗位"
        min-width="120"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.recruitDetail">{{ scope.row.recruitDetail.station }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="department"
        label="所在部门"
        min-width="120"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.recruitDetail">{{ scope.row.recruitDetail.department }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="负责人"
        min-width="120"
      >
        <template slot-scope="scope">
          {{ scope.row.name || '-' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="requirement"
        label="需求类型"
        min-width="120"
      >
        <template slot-scope="scope">
          {{ getRequirementText(scope.row.requirement) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="recruitDetail.number"
        label="需求人数"
        width="60"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.recruitDetail">{{ scope.row.recruitDetail.number }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="processType"
        label="流程类型"
        width="100"
      >
        <template slot-scope="scope">
          <el-tag :type="getProcessTypeTag(scope.row.state)">
            {{ getProcessTypeText(scope.row.state) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="state"
        label="状态"
        width="100"
      >
        <template slot-scope="scope">
          <el-tag :type="getStateType(scope.row.state)">
            {{ getStateText(scope.row.state) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- 招聘流程: 状态为1-2时 -->
          <div v-if="scope.row.state === 1 || scope.row.state === 2" class="operation-buttons">
            <el-button
              size="mini"
              type="text"
              @click.stop="handleDetail(scope.row)"
            >
              <i class="el-icon-document"></i>
              详情
            </el-button>
            
            <el-button
              v-if="scope.row.state === 1"
              size="mini"
              type="text"
              @click.stop="handleApproval(scope.row)"
            >
              <i class="el-icon-s-check"></i>
              审批
            </el-button>
            
            <el-button
              v-if="scope.row.state === 2"
              size="mini"
              type="text"
              @click.stop="handleRecruit(scope.row)"
            >
              <i class="el-icon-user"></i>
              招聘
            </el-button>
          </div>
          
          <!-- 面试流程: 状态为3时 -->
          <div v-if="scope.row.state === 3" class="operation-buttons">
            <el-button
              size="mini"
              type="text"
              @click.stop="handleDetail(scope.row)"
            >
              <i class="el-icon-document"></i>
              详情
            </el-button>
            
            <el-button
              size="mini"
              type="text"
              @click.stop="handleInterview(scope.row)"
            >
              <i class="el-icon-user"></i>
              面试
            </el-button>
          </div>
          
          <!-- 已通过: 状态为4时 -->
          <div v-if="scope.row.state === 4" class="operation-buttons">
            <el-button
              size="mini"
              type="text"
              @click.stop="handleDetail(scope.row)"
            >
              <i class="el-icon-document"></i>
              查看
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <pagination
      :current-page.sync="pagination.current"
      :page-size.sync="pagination.size"
      :total="pagination.total"
      @pagination-change="fetchDemandData"
    />
  </div>
</template>

<script>
import Pagination from '@/components/common/Pagination.vue'

export default {
  name: 'RecruitmentDemands',
  components: {
    Pagination
  },
  data() {
    return {
      loading: false,
      searchForm: {
        departName: '',
        stationName: ''
      },
      tableData: [],
      pagination: {
        current: 1,
        size: 10,
        total: 0
      }
    }
  },
  created() {
    this.fetchDemandData()
  },
  methods: {
    // 获取需求数据
    async fetchDemandData() {
      try {
        this.loading = true
        const params = {
          current: this.pagination.current,
          size: this.pagination.size,
          departName: this.searchForm.departName || undefined,
          stationName: this.searchForm.stationName || undefined
        }
        
        const response = await this.$axios.get('/recruitment/recruit', { params })
        
        if (response && response.result === 200) {
          const { records, total, size, current } = response.data
          this.tableData = records
          this.pagination = {
            total,
            size,
            current
          }
        } else {
          this.$message.error(response.message || '获取需求数据失败')
        }
      } catch (error) {
        console.error('获取需求数据出错：', error)
        this.$message.error('获取需求数据失败')
      } finally {
        this.loading = false
      }
    },

    // 搜索
    handleSearch() {
      this.pagination.current = 1
      this.fetchDemandData()
    },

    // 重置搜索
    resetSearch() {
      this.searchForm = {
        departName: '',
        stationName: ''
      }
      this.handleSearch()
    },

    // 获取需求类型文本
    getRequirementText(type) {
      const map = {
        1: '离职补充',
        2: '调动补充',
        3: '人员储备',
        4: '岗位扩编',
        5: '临时用工'
      }
      return map[type] || '未知'
    },

    // 获取状态文本
    getStateText(state) {
      const map = {
        0: '草稿',
        1: '待审批',
        2: '初筛通过',
        3: '面试中',
        4: '已通过',
        5:'被驳回'
      }
      return map[state] || '未知'
    },

    // 获取状态标签类型
    getStateType(state) {
      const map = {
        0: 'info',
        1: 'warning',
        2: 'success',
        3: 'danger',
        4: ''
      }
      return map[state] || ''
    },

    // 获取流程类型文本
    getProcessTypeText(state) {
      if (state >= 0 && state <= 2) {
        return '招聘流程'
      } else if (state >= 3 && state <= 4) {
        return '面试流程'
      } else {
        return '未知'
      }
    },

    // 获取流程类型标签样式
    getProcessTypeTag(state) {
      if (state >= 0 && state <= 2) {
        return 'primary'
      } else if (state >= 3 && state <= 4) {
        return 'success'
      } else {
        return 'info'
      }
    },

    // 查看详情
    handleDetail(row) {
      console.log('查看详情:', row)
      this.$router.push({
        path: '/recruitment/process-detail',
        query: { insId: row.proInsId }
      })
    },

    // 处理审批
    handleApproval(row) {
      this.$router.push({
        path: '/recruitment/approval',
        query: { id: row.id,proInsId: row.proInsId }
      })
      console.log(row)
    },

    // 处理招聘
    handleRecruit(row) {
      // 准备映射数据
      const mappedData = {
        recruitId:row.id,
        station: row.recruitDetail.station || '', // 需求岗位名称 -> 职位名称
        education: row.recruitDetail.education || '', // 学历要求 -> 学历要求
        requirements: row.recruitDetail.otherRequirements || '', // 任职要求 -> 任职要求
        skills: row.recruitDetail.skills || '' // 技能要求 -> 岗位职责
      }
      
      // 跳转到招聘页面并携带数据
      this.$router.push({
        path: '/recruitment/add-position',
        query: mappedData
      })
    },
    handleInterview(row) {
      console.log('查看详情:', row)

      this.$router.push({
        path: '/recruitment/interview-start',
        query: { id: row.id, proInsId: row.proInsId,row: row }
      })
    }
  }
}
</script>

<style scoped>
.demands-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  min-height: calc(100vh - 120px);
}

.search-bar {
  margin-bottom: 20px;
}

.search-form {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

:deep(.el-table) {
  margin-top: 20px;
}

:deep(.el-form-item) {
  margin-bottom: 0;
  margin-right: 20px;
}

/* 可点击行的样式 */
:deep(.el-table__row) {
  cursor: pointer;
}

:deep(.el-table__row:hover) {
  background-color: #f5f7fa;
}

/* 按钮间距 */
.el-button--text {
  margin-right: 8px;
}

/* 按钮图标 */
.el-button [class^="el-icon-"] {
  margin-right: 4px;
}

/* 操作按钮样式 */
.operation-buttons {
  display: flex;
  align-items: center;
}

.operation-buttons .el-button {
  margin-left: 5px;
  margin-right: 5px;
  padding: 2px 5px;
}

.operation-buttons .el-button:first-child {
  margin-left: 0;
}
</style> 